<html>
<head><meta charset="utf-8"><title>Hash of slices including the length · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html">Hash of slices including the length</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241937523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241937523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241937523">(Jun 08 2021 at 15:25)</a>:</h4>
<p>I'd made &lt;<a href="https://github.com/rust-lang/rust/pull/86131">https://github.com/rust-lang/rust/pull/86131</a>&gt; to stop hashing the length for arrays, but was reminded that that's not allowed because of <code>Borrow</code>.</p>
<p>That made me wonder how useful it is to have the length included in the hashes for slices either.  I guess it's to avoid length extension attacks?</p>



<a name="241937735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241937735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241937735">(Jun 08 2021 at 15:26)</a>:</h4>
<p>doesn't the hash function itself record the length?</p>



<a name="241937903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241937903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241937903">(Jun 08 2021 at 15:27)</a>:</h4>
<p>I guess it makes there be a difference between hashing <code>(vec![1], vec![2, 3, 4])</code> and <code>(vec![1, 2], vec![3, 4])</code>.</p>



<a name="241938046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241938046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241938046">(Jun 08 2021 at 15:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="330154">The 8472</span> <a href="#narrow/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length/near/241937735">said</a>:</p>
<blockquote>
<p>doesn't the hash function itself record the length?</p>
</blockquote>
<p>I'm not sure what you're asking here.  The implementation of <code>Hash</code> for <code>[T]</code> hashes the length of the slice, then the elements.</p>



<a name="241938100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241938100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241938100">(Jun 08 2021 at 15:29)</a>:</h4>
<p>hashing just a u8 would be enough for that, though, right? Essentially a null delimiter. Is there a reason we need the length specifically?</p>



<a name="241938132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241938132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241938132">(Jun 08 2021 at 15:29)</a>:</h4>
<p>(it may be more efficient to hash a single byte)</p>



<a name="241938702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241938702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241938702">(Jun 08 2021 at 15:32)</a>:</h4>
<blockquote>
<p>I'm not sure what you're asking here.</p>
</blockquote>
<p>Well, at least in cryptography preventing length extension attacks usually is implemented in the hash function itself. But yeah, if the structure is relevant and not just the raw bytes being hashed then that's different.</p>



<a name="241940568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241940568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241940568">(Jun 08 2021 at 15:44)</a>:</h4>
<p>reminds me of <a href="https://github.com/rust-lang/rust/issues/80303">#80303</a></p>



<a name="241943700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241943700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241943700">(Jun 08 2021 at 16:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="330154">The 8472</span> <a href="#narrow/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length/near/241938702">said</a>:</p>
<blockquote>
<p>Well, at least in cryptography preventing length extension attacks usually is implemented in the hash function itself. But yeah, if the structure is relevant and not just the raw bytes being hashed then that's different.</p>
</blockquote>
<p>Ah, I follow now.  I think it's up to the specific <code>Hasher</code> to decide if it wants to hash the total length at the end; I don't know if <code>DefaultHaser</code> or any of the others in common use actually do.</p>



<a name="241961514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241961514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241961514">(Jun 08 2021 at 18:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length/near/241937903">said</a>:</p>
<blockquote>
<p>I guess it makes there be a difference between hashing <code>(vec![1], vec![2, 3, 4])</code> and <code>(vec![1, 2], vec![3, 4])</code>.</p>
</blockquote>
<p>As well as a difference between hashing <code>vec![1, 2, 3]</code> and hashing <code>(vec![1, 2], 3)</code>.</p>



<a name="241963517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241963517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241963517">(Jun 08 2021 at 18:19)</a>:</h4>
<p>Well, if heterogeneous is allowed there's still the <code>(9, [3])</code> and <code>(9, 1, 3)</code>  kinds of conflict.</p>



<a name="241963782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241963782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241963782">(Jun 08 2021 at 18:21)</a>:</h4>
<p>Or even just <code>(1, (2, 3))</code> vs <code>((1, 2), 3)</code>.</p>



<a name="241964156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241964156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241964156">(Jun 08 2021 at 18:24)</a>:</h4>
<p>those are different types though. the homogenous two-vecs-next-to-eachother is the motivation for the length prefix iirc</p>



<a name="241987134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/241987134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#241987134">(Jun 08 2021 at 21:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> <a href="#narrow/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length/near/241938100">said</a>:</p>
<blockquote>
<p>hashing just a u8 would be enough for that, though, right? Essentially a null delimiter. Is there a reason we need the length specifically?</p>
</blockquote>
<p>That would cause clashes between <code>([1u8], [0u8, 2u8])</code> and <code>([1u8, 0u8], [2u8])</code></p>



<a name="242024229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/242024229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#242024229">(Jun 09 2021 at 06:59)</a>:</h4>
<p>Spitballing: It could LEB128, or something, the length to reduce the number of bytes needing hashing.  That'd help the <code>[u8; 3]</code> case somewhat, though it's unclear that it'd be worth it overall.</p>



<a name="242026379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/242026379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#242026379">(Jun 09 2021 at 07:31)</a>:</h4>
<p>for <code>[T; N]</code> the length shouldn't need to be hashed. Only for <code>[T]</code> is it necessary.</p>



<a name="242030668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/242030668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#242030668">(Jun 09 2021 at 08:19)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> That's what I tried to do first, but was reminded that it does have to be hashed to match slices because of &lt;<a href="https://doc.rust-lang.org/std/borrow/trait.Borrow.html#impl-Borrow%3C%5BT%5D%3E-1">https://doc.rust-lang.org/std/borrow/trait.Borrow.html#impl-Borrow%3C%5BT%5D%3E-1</a>&gt;.</p>



<a name="242038064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Hash%20of%20slices%20including%20the%20length/near/242038064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Hash.20of.20slices.20including.20the.20length.html#242038064">(Jun 09 2021 at 09:27)</a>:</h4>
<p>Ah, I see.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>